home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_texblender.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  5.6 KB  |  213 lines

  1.  
  2.  
  3. #include "k3d_filterwidth.h"
  4. #include "k3d_project.h"
  5. #include "k3d_displace.h"
  6. #include "k3d_material.h"
  7. #include "k3d_reflections.h"
  8. #include "k3d_locillum.h"
  9. #include "k3d_noises.h"
  10.  
  11. color altMaterialPlastic (normal Nf;  color basecolor,selftcolor,Rcolor,RFcolor;
  12.                        float Ka, Kd, Ks, roughness,selft,Kr,Krfr;)
  13. {
  14.     extern vector I;
  15.     return (basecolor * ( Ka*ambient() + Kd*diffuse(Nf)+selft*selftcolor ) )
  16.        + Kr*Rcolor+Krfr*RFcolor
  17.      + Ks*specular(Nf,-normalize(I),roughness);
  18. }
  19.  
  20. color
  21. myEnvironment (point P;  vector R;  float Kr, blur;  DECLARE_ENVPARAMS;)
  22. {
  23.     color C = 0;
  24.     float alpha;
  25.     if (envname != "") {
  26.         if (envspace == "NDC")
  27.             C = ReflMap (envname, P, blur, alpha);
  28.         else
  29.             C = Environment (envname, envspace, envrad, P, R, blur, alpha);
  30.     }
  31.         else
  32.         {
  33. #if (defined(BMRT) || defined(RAYSERVER_H))
  34.     color Cray = RayTrace (P, R, Kr, sqrt(blur), rayjitter, raysamples, alpha);
  35.     C = Cray + (1-alpha) * C;
  36. #endif
  37.         }
  38.     return C;
  39. }
  40.  
  41. float myrand(point p;)
  42. {
  43.     float x,hx;
  44.     x=abs(xcomp(p));
  45.     x+=abs(ycomp(p));
  46.     x+=abs(zcomp(p));
  47.  
  48.     float n=log(x,10);
  49.     n-=6;
  50.     n=round(n);
  51.     x=x/pow(10,n);
  52.     hx=mod(x,1000000)/1000;
  53.     x=mod(x+hx+x*100,1000);
  54.     return x/1000;
  55. }
  56.  
  57. float tex_clouds(point P;float depth)
  58. {
  59.     float val=0;
  60.     float i,arm=1;
  61.     float Karm=1;
  62.  
  63.     for(i=0;i<depth;i=i+1)
  64.     {
  65.         //val=val*(1-Karm)+Karm*snoise(arm*P);
  66.         val=val+Karm*snoise(arm*P);
  67.         Karm*=0.8;
  68.         arm*=2;
  69.     }
  70.     return clamp((val+1)/2,0,1);
  71. }
  72.  
  73. float tex_wood(point PP; float ringscale)
  74. {
  75.     float grainy = 1;
  76.  
  77.     float my_t = zcomp(PP) / ringscale;
  78.     point PQ = point (xcomp(PP)*8, ycomp(PP)*8, zcomp(PP));
  79.     my_t += noise (PQ) / 16;
  80.  
  81.     PQ = point (xcomp(PP), my_t, ycomp(PP)+12.93);
  82.     float r = ringscale * noise (PQ);
  83.     r -= floor (r);
  84.     r = 0.2 + 0.8 * smoothstep(0.2, 0.55, r) * (1 - smoothstep(0.75, 0.8, r));
  85.     PQ = point (xcomp(PP)*128+5, zcomp(PP)*8-3, ycomp(PP)*128+1);
  86.     float r2 = grainy * (1.3 - noise (PQ)) + (1-grainy);
  87.  
  88.     return clamp(r*r2*r2,0,1);
  89. }
  90.  
  91. color colorMap(string mapname, space; 
  92.         float scalex, scaley, scalez, octaves, blur;)
  93. {
  94.     point transp;
  95.     color newc;
  96.  
  97.     transp=transform(space,P)*vector (scalex, scaley, scalez);
  98.  
  99.     if (mapname=="clouds")
  100.         newc=tex_clouds(transp,octaves);
  101.     else if (mapname=="noise")
  102.         newc=myrand(transp);
  103.     else if (mapname=="wood")
  104.         newc=tex_wood(transp,octaves);
  105.     else 
  106.     {
  107.         newc=texture(mapname,s*scalex,t*scaley,"blur",blur,"fill",-1,"width",0);
  108.     if (comp(newc,1)==-1) newc=comp(newc,0); /* treat a 1-channel texture map as a greyscale */
  109.     }
  110.               return newc;
  111. }
  112.  
  113. color BlendColor(string mode; float K; color newc, oldc)
  114. {
  115.   color res=oldc;
  116.   if (mode=="m") res+=K*newc;
  117.   if (mode=="f") res*= (1-K)*(color 1)+K*newc;
  118.     return res;
  119. }
  120.  
  121. float BlendFloat(string mode; float K, newf, oldf)
  122. {
  123.   float res=oldf;
  124.  
  125.  if (mode=="m") res=(res+K*newf)/(1+K);
  126.  if (mode=="f") res*= ((1-K)+K*newf);
  127.  
  128.     return res;
  129. }
  130.  
  131. surface
  132. k3d_texblender (float Ka = 1, Kd = .5, Ks = .2, roughness = .3, Kr=0,selft=0;
  133.         float Rblur=0.1,RFblur=0.1;
  134.         float Krfr=0,eta=0.8,Ko=1;
  135.  
  136.         float cKmap[5]={0,0,0,0,0};
  137.         float oKmap[5]={0,0,0,0,0};
  138.         float sKmap[5]={0,0,0,0,0};
  139.         float mKmap[5]={0,0,0,0,0};
  140.         float bKmap[5]={0,0,0,0,0};
  141.         float rKmap[5]={0,0,0,0,0};
  142.         float iKmap[5]={0,0,0,0,0};
  143.         float aKmap[5]={0,0,0,0,0};
  144.         float nGmap=0;
  145.         float Kcs=1;
  146.         float Sgmx[5]={1,1,1,1,1},Sgmy[5]={1,1,1,1,1},Sgmz[5]={1,1,1,1,1};
  147.         float Depth[5]={3,3,3,3,3};
  148.         float Gblur[5]={0,0,0,0,0};
  149.         string Gmapname[5]={"","","","",""};
  150.         string Gspace[5]={"object","object","object","object","object"};
  151.         string Gmode[5]={"m","m","m","m","m"};
  152.  
  153.         DECLARE_DEFAULTED_ENVPARAMS;
  154.     )
  155. {
  156.     color Ct = Kcs*Cs, Ot = Os*Ko,Cselft=Kcs*Cs;
  157.     float ks = Ks,kr=Kr;
  158.     float disp = 0,i,Alpha=1;
  159.     vector V ,D,badN,corr,dispDir,ndir;
  160.  
  161.     normal Nf =normalize( faceforward(normalize(N),I));
  162.     dispDir=normalize( faceforward(normalize(Ng),I));
  163.     corr=Nf-dispDir;
  164.  
  165.     for(i=0;i<nGmap;i=i+1)
  166.     {
  167.         
  168.         color MC=colorMap(Gmapname[i],Gspace[i],Sgmx[i],Sgmy[i],Sgmz[i],
  169.                                             Depth[i],Gblur[i]);
  170.     float MF=(comp(MC,0)+comp(MC,1)+comp(MC,2))/3; // when we want a float
  171.         string mode=Gmode[i];
  172.         
  173.         if((Alpha*cKmap[i])!=0) Ct=BlendColor(mode,Alpha*cKmap[i],MC,Ct);
  174.         if((Alpha*oKmap[i])!=0) Ot=BlendColor(mode,Alpha*oKmap[i],MC,Ot);
  175.         if((Alpha*sKmap[i])!=0) ks=BlendFloat(mode,Alpha*sKmap[i],MF,ks);
  176.         if((Alpha*mKmap[i])!=0) kr=BlendFloat(mode,Alpha*mKmap[i],MF,kr);
  177.         if((Alpha*rKmap[i])!=0) roughness=BlendFloat(mode,Alpha*rKmap[i],
  178.                                                  MF,roughness);
  179.         if(aKmap[i]!=0) Alpha=BlendFloat("f",aKmap[i],MF,1);
  180.         if((Alpha*iKmap[i])!=0) selft=BlendFloat(mode,Alpha*iKmap[i],MF,selft);
  181.         if((Alpha*bKmap[i])!=0) disp=BlendFloat(mode,Alpha*bKmap[i],MF,disp);
  182.     }
  183.     if(disp!=0)
  184.     {
  185.             ndir=normalize(corr+Displace(dispDir,"shader",disp,0));
  186.             ndir=normalize(ndir-(Nf*(ndir.Nf)));
  187.             ndir=normalize(Nf+ndir);
  188.             ndir=normalize(ndir-(Nf*(ndir.Nf)));
  189.             Nf+=disp*(normalize(ndir));
  190.             Nf=normalize(Nf);
  191.     }
  192.  
  193.     color env=0,benv=0;
  194.     float olds=raysamples;
  195.     V = normalize(I);
  196.     
  197.     if((kr!=0) && (((Nf.V)>=0) || (raylevel()==0)) ) 
  198.     {
  199.         if(Rblur==0) raysamples=1;
  200.         env=myEnvironment(P,normalize(reflect(V,Nf)),1,Rblur,ENVPARAMS);
  201.     }
  202.     if(Krfr!=0) 
  203.     {
  204.         if(RFblur==0) raysamples=1;
  205.         else raysamples=olds;
  206.         benv=myEnvironment(P,refract(V,Nf,(V.Nf > 0) ? 1.0/eta : eta),1,RFblur,ENVPARAMS);
  207.     }
  208.     Ci = altMaterialPlastic (Nf,Ct,Cselft,env,benv,
  209.                 Ka,Kd,ks,roughness,selft,kr,Krfr);
  210.     Oi = Ot;  Ci *= Oi;
  211. }
  212.  
  213.